package com.cms.controllers.admin;

import com.cms.common.aop.permission.PermissionCheck;
import com.cms.common.model.entity.ArticleCategory;
import com.cms.common.model.entity.Translate;
import com.cms.common.model.services.ArticleCategoryService;
import com.cms.common.model.services.TranslateService;
import com.cms.common.model.vo.RequestData;
import com.cms.common.model.vo.ResponseResult;
import com.cms.controllers.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/article_category")
public class ArticleCategoryController extends BaseController {

    @Autowired
    ArticleCategoryService articleCategoryService;
    @Autowired
    TranslateService translateService;

    @GetMapping("")
    @PermissionCheck(name = "分类列表", typeName = "文章分类管理")
    public String index(Model viewModel) {
        return "admin/article/category/index";
    }

    @GetMapping("/list")
    @ResponseBody
    @PermissionCheck(name = "分类列表数据", typeName = "文章分类管理")
    public ResponseResult categories(@ModelAttribute RequestData requestData) {
        Page<ArticleCategory> articleCategories = articleCategoryService.getAllByPage(requestData);
        return ResponseResult.success(articleCategories);
    }

    @PostMapping("/store")
    @ResponseBody
    @PermissionCheck(name = "创建分类", typeName = "文章分类管理")
    public ResponseResult store(@RequestParam("name") String name) {
        if (name.isEmpty()) {
            ResponseResult.fail("名字不能为空");
        }

        ArticleCategory articleCategory = new ArticleCategory();
        Translate translate = new Translate();
        translate.setContent(name);
        List<Translate> translates = new ArrayList<>();
        translates.add(translate);
        translates = translateService.addTranslates(translates);
        articleCategory.setNameTranslates(translates);

        articleCategory = articleCategoryService.addArticleCategory(articleCategory);
        return ResponseResult.success(articleCategory);
    }

    @PutMapping("/update/{id}")
    @ResponseBody
    @PermissionCheck(name = "修改分类", typeName = "文章分类管理")
    public ResponseResult update(@PathVariable Long id, @RequestParam("name") String name) {

        ArticleCategory articleCategory = articleCategoryService.getById(id);
        if (articleCategory == null) {
            ResponseResult.fail("分类不存在");
        }
        translateService.deleteTranslates(articleCategory.getNameTranslates());
        Translate translate = new Translate();
        translate.setContent(name);

        List<Translate> translates = new ArrayList<>();
        translates.add(translate);
        translates = translateService.addTranslates(translates);
        articleCategory.setNameTranslates(translates);
        articleCategory = articleCategoryService.addArticleCategory(articleCategory);
        return ResponseResult.success(articleCategory);
    }

    @DeleteMapping("/destroy/{id}")
    @ResponseBody
    @PermissionCheck(name = "删除分类", typeName = "文章分类管理")
    public ResponseResult destroy(@PathVariable Long id) {
        ArticleCategory articleCategory = articleCategoryService.getById(id);
        if (articleCategory == null) {
            ResponseResult.fail("分类不存在");
        }
        translateService.deleteTranslates(articleCategory.getNameTranslates());
        articleCategoryService.deleteArticleCategory(articleCategory);
        return ResponseResult.success("删除成功");
    }
}
